package org.jeecg.modules.cascv.image.tools;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.HexUtil;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.modules.cascv.image.util.HeaderUtil;
import org.jeecg.modules.cascv.image.util.NumberUtil;

import java.io.RandomAccessFile;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author;作者
 * @Date 2023/3/28 15:30
 * @Description :
 * @Version
 */
public class liangxiangudp {
    public static void main(String[] args) throws Exception {
//        RandomAccessFile randomAccessFile = new RandomAccessFile("D:\\pc_workspace\\数据\\两相柜\\" +
//                "T20210429174006_0001_009c_F1_00_6DCD_2024_0040_40_6D1D_000000000.dat", "r");
//        //T20210429174006_0001_009c_F1_00_6DCD_2024_0040_40_6D1D_000000000.dat.bnt
//      //  byte[] bytes = new byte[2024];
//        //randomAccessFile.seek(820*2+420*2);
//        byte[] bytes1 = new byte[820];
//        byte[] bytes2 = new byte[420];
//        long offset= 0;
//        JSONObject jsonObject = new JSONObject();
//        JSONObject content = new JSONObject();
//        String str="";
//        while(true){
//
//
//                int read = randomAccessFile.read(bytes1);
//                if (read <= 0) {
//                    break;
//                }
//                str = HexUtil.encodeHexStr(bytes1);
//            int wwaaIndexOf = str.lastIndexOf("55aa");
//            if(str.startsWith("55aa")&&wwaaIndexOf==0){
//                    //randomAccessFile.seek(offset);
//                offset += 820;
//                jsonObject.put("tableName", "TGMTYY1401_09CH_31_TPSRAPP1UDP");
//                jsonObject.put("Content", content);
//                content.put("A1_TPSR_MICU_APP1DATA_32_2021_1", str);
//                         FileUtil.appendUtf8String(jsonObject.toString() + System.lineSeparator(),
//                               "D:\\pc_workspace\\数据\\两相柜\\" +
//                                       "TGMTYY1401_09CH_31_TPSRAPP1UDP1.txt");
//                }else if(str.startsWith("55aa")&&wwaaIndexOf!=0){
//                randomAccessFile.seek(offset);
//                read = randomAccessFile.read(bytes2);
//                if (read <= 0) {
//                    break;
//                }
//                str = HexUtil.encodeHexStr(bytes2);
//                offset += 420;
//                jsonObject.put("tableName", "TGMTYY1401_09CH_31_TPSRAPP1UDP");
//                jsonObject.put("Content", content);
//                content.put("A1_TPSR_MICU_APP1DATA_32_2021_1", str);
//                FileUtil.appendUtf8String(jsonObject.toString() + System.lineSeparator(),
//                        "D:\\pc_workspace\\数据\\两相柜\\" +
//                                "TGMTYY1401_09CH_31_TPSRAPP1UDP1.txt");
//            }
            //封装json数据
            //推送kafka数据
            byte[] bytes = FileUtil.readBytes("\\pc_workspace\\数据\\两相柜\\" +
                    "T20210429174006_0001_009c_F1_00_6DCD_2024_0040_40_6D1D_000000000.dat");
            String s = HexUtil.encodeHexStr(bytes);
            String[] split = s.split("55aa");
            JSONObject jsonObject = new JSONObject();

        for (String s1 : split) {
                if(StringUtils.isEmpty(s1)){
                    continue;
                }
           String data =  "55aa" + s1;
            String substring = data.substring(32, data.length() - 4);

            jsonObject.put("tableName","TGMTYY1401_09CH_31_TPSRAPPUDP1");
            jsonObject.put("TGMTYY1401_09CH_31_TPSRAPPUDP1",substring);
    //            jsonObject.put("tableName","TGMTYY1401_09CH_31_TPSRAPP1UDP");

                FileUtil.appendUtf8String(jsonObject.toString() + System.lineSeparator(),
                        "D:\\pc_workspace\\数据\\两相柜\\" +
                                "TGMTYY1401_09CH_31_TPSRAPP1UDP_1.txt");
            }

    }
    /**
     * 秒数转天时分秒
     */
    public static String toDayHoursMinSec(Long time){
        long day = TimeUnit.SECONDS.toDays(time);
        long hours = TimeUnit.SECONDS.toHours(time) - TimeUnit.DAYS.toHours(TimeUnit.SECONDS.toDays(time));
        long minutes = TimeUnit.SECONDS.toMinutes(time) - TimeUnit.HOURS.toMinutes(TimeUnit.SECONDS.toHours(time));
        long seconds = TimeUnit.SECONDS.toSeconds(time) - TimeUnit.MINUTES.toSeconds(TimeUnit.SECONDS.toMinutes(time));
        // 172800+14400+1720
        return day+":"+hours+":"+minutes+":"+seconds;
    }


    /**
     *    //     Integer second = NumberUtil.versbyte2Integer(HexUtil.decodeHex(secondStr), 0);
     *         //    content.put("A1_TPSR_MICU_APP1DATA_6_7_2",packageNo+";"+packageNo+";包序号");//序列标志
     *       //      content.put("A1_TPSR_MICU_APP1DATA_8_11_1", secondStr + "H;" + HeaderUtil.getShipSecond(second.longValue()) + ";时间戳秒;否");//秒值
     *         //    content.put("A1_TPSR_MICU_APP1DATA_12_13_1",subSecondStr + "H;" + NumberUtil.versbyte22Integer(HexUtil.decodeHex(subSecondStr), 0) * 40 + ";时间戳亚秒;否");//微秒值
     *           //  content.put("A1_TPSR_MICU_APP1DATA_6_7_1",startFlagStr+"B;"+startFlagStr+"B;序列标识");//序列标志
     *        //     content.put("A1_TPSR_MICU_APP1DATA_32_2021_1","null;0x" + dataStr + ";null;null");//数据帧
     *        //     content.put("A1_TPSR_MICU_APP1DATA_31_31_1",taskNoStr + "H;" + Integer.parseUnsignedInt(taskNoStr, 16) + ";任务号;否");//任务号
     *         //    content.put("A1_TPSR_MICU_APP1DATA_22_25_1",Integer.parseUnsignedInt(imageNoStr, 16) + ";图像序号;否");//
     *         //    content.put("A1_TPSR_MICU_APP1DATA_26_27_1",xStr + "H;" + x + ";分辨率X;否");//
     *          //   content.put("A1_TPSR_MICU_APP1DATA_28_29_1",yStr + "H;" + y + ";分辨率Y;否");//
     *             //A1_GCSR-MICU_APP1DATA_20-2021_1
     */
}
